local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHScreenBase());
local CommonOHOH =_G.CommonOHOH
local CallBackManager = ClientOHOH.load.CallBackManager();


function InitInsts(ui)
	ui.insts = {};
	ui.insts.offon = "root.TextButton";
	ui.insts.cell = "root.ScrollingFrame.Frame.cell";
	ui.insts.content = "root.ScrollingFrame.Frame";
	ui.insts.show = "root.ScrollingFrame";

end
function M.MobileSize(self)
	if _G.client.IsMobile then --是移动设备
		if self.insts.root then
			local pos= self.insts.root:GetAttribute("Position")
			local size=self.insts.root:GetAttribute("Size")

			if pos then
				self.insts.root.Position=pos
			end
			if size then
				self.insts.root.Size=size
			end
		end
	end
end

function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self)
	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.TestGui"));
	self:MobileSize()
	
	self.insts.show.Visible=false
	
	CallBackManager:AddRbxConnect(self, self.insts.offon.Activated, function()
		self.insts.show.Visible=not self.insts.show.Visible
	end);
	
end;

function M.BindButton(self,button,data)
	
	local arg= button:WaitForChild("TextBox")
	local action= button:WaitForChild("TextButton")
	action.Text=data.buttonName
	CallBackManager:AddRbxConnect(self, action.Activated, function()
		self.callBack(data,arg.Text)
	end);
end

function M.InitUIGridLay(self, scorlling, grid, p34)

	assert(scorlling, "param scorlling frame is nil");
	assert(grid, "param ui grid layout is nil");
	assert(scorlling:IsA("ScrollingFrame"), "param scorlling frame must be a ScorllingFrame");
	assert(grid:IsA("UIGridLayout"), "param ui grid layout must be a UIGridLayout");

	CallBackManager:AddRbxConnect(self, grid.Changed, function(p35, p36)
		if p36 == "AbsoluteCellCount" then

			--print("InitUIGridLayInitUIGridLay-->",p35,p36)

			local scale_g2s_x = grid.AbsoluteContentSize.X / scorlling.AbsoluteCanvasSize.X;
			local scale_g2s_y = grid.AbsoluteContentSize.Y / scorlling.AbsoluteCanvasSize.Y;
			local s_x = scorlling.CanvasSize.X.Scale * scale_g2s_x;
			local s_y = scorlling.CanvasSize.Y.Scale * scale_g2s_y;
			if s_x <= scorlling.CanvasSize.X.Scale and s_y <= scorlling.CanvasSize.Y.Scale then
				return;
			end;
			local g_x = grid.CellSize.X.Scale / scale_g2s_x;
			local g_y = grid.CellSize.Y.Scale / scale_g2s_y;
			local p_x = grid.CellPadding.X.Scale / scale_g2s_x;
			local p_y = grid.CellPadding.Y.Scale / scale_g2s_y;
			if p34 then
				if scorlling.ScrollingDirection == Enum.ScrollingDirection.X then
					s_y = scorlling.CanvasSize.Y.Scale;
					g_y = grid.CellSize.Y.Scale;
					p_y = grid.CellPadding.Y.Scale;
				elseif scorlling.ScrollingDirection == Enum.ScrollingDirection.Y then
					s_x = scorlling.CanvasSize.X.Scale;
					g_x = grid.CellSize.X.Scale;
					p_x = grid.CellPadding.X.Scale;
				end;
			end;
			scorlling.CanvasSize = UDim2.fromScale(s_x, s_y);
			grid.CellSize = UDim2.fromScale(g_x, g_y);
			grid.CellPadding = UDim2.fromScale(p_x, p_y);
		end;
	end);
end;

function M.InitTestData(self,data,callBack)
	self.callBack=callBack
	local isUsedPerfab=false
	for k,v in pairs(data) do
		local button=nil
		if isUsedPerfab then
			button=self.insts.cell:Clone()
			button.Parent=self.insts.cell.Parent
		else
			button=self.insts.cell
			isUsedPerfab=true
		end
		self:BindButton(button,v)
	end
	
end

function M.dtor(self)
	M.super.dtor(self);
end;

return M
